<?php
if (!defined('BASEPATH'))
	exit ('No direct script access allowed');

class Hang extends CI_Controller {
	var $data;
	function __construct() {
		parent :: __construct();
		if ($this->session->userdata('userid') == FALSE) {
			Header("Location:" . site_url('welcome'));
			exit ();
		}
		$this->data['username'] = $this->session->userdata('username');
		$this->load->model('hangs');
	}
	function index($pageindex=0) {
		parse_str($_SERVER['QUERY_STRING'], $_GET);
		$types = isset ($_GET['types']) ? $_GET['types'] : '2';
		$pageindex = intval($pageindex);
		$this->load->library('pagination');
		$config['base_url'] = site_url('admin/info/index');
		$config['total_rows'] = $this->hangs->getCount($types);
		$config['per_page'] = 10;
		$config['uri_segment'] = 4;
		$config['num_links'] = 10;
		$config['first_link'] = '首页';
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$config['last_link'] = '尾页';
		$this->pagination->initialize($config);
		$this->data['page'] = $this->pagination->create_links();
		$this->data['list'] = $this->hangs->getLists($pageindex, $config['per_page'],$types);
		$this->data['type_name'] = $types;
		$this->load->view('admin/hang', $this->data);
	}
	function add () {
		$this->load->view('admin/hang_add');
	}
	function edit () {
		parse_str($_SERVER['QUERY_STRING'], $_GET);
		$id = isset ($_GET['id']) ? $_GET['id'] : '';
		$info = $this->hangs->select_info($id);
		$this->load->view('admin/hang_edit',$info);
	}
	function insert() {
		$this->datas['title'] = $_POST['title'];
		$this->datas['class'] = $_POST['class'];
		$this->datas['new'] = $_POST['new'];
		$this->datas['buy'] = $_POST['buy'];
		$this->datas['sall'] = $_POST['sall'];
		$this->db->insert('hang', $this->datas);
		$this->message['message_info'] = '信息添加成功，将回到添加页面继续添加！';
		$this->message['message_url'] = site_url('admin/hang');
		$this->load->view('admin/message', $this->message);
	}
	function updates() {
			$this->data['title'] = $_POST['title'];
			$this->data['id'] = $_POST['id'];
			$this->data['buy'] = $_POST['buy'];
			$this->data['sall'] = $_POST['sall'];
			$this->data['class'] = $_POST['class'];
			$this->data['new'] = $_POST['new'];
			$this->hangs->update_info('hang', 'buy', $this->data['buy'], $this->data['id']);
			$this->hangs->update_info('hang', 'sall', $this->data['sall'], $this->data['id']);
			$this->hangs->update_info('hang', 'class', $this->data['class'], $this->data['id']);
			$this->hangs->update_info('hang', 'new', $this->data['new'], $this->data['id']);
			$this->hangs->update_info('hang', 'title', $this->data['title'], $this->data['id']);
			$this->message['message_info'] = '信息更新成功！';
			$this->message['message_url'] = site_url('admin/hang/edit?id=' . $this->data['id']);
			$this->load->view('admin/message', $this->message);
	}
	function delete() {
		parse_str($_SERVER['QUERY_STRING'], $_GET);
		$id = isset ($_GET['id']) ? $_GET['id'] : '';
		$this->hangs->delInfo($id);
		$this->message['message_info'] = '删除成功！';
			$this->message['message_url'] = site_url('admin/hang');
			$this->load->view('admin/message', $this->message);
	}

}